package com.dycx.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.user.dto.IndustryTypeDTO;
import com.dycx.user.entity.IndustryTypeDO;
import com.dycx.user.mapper.IndustryTypeMapper;
import com.dycx.user.service.IndustryTypeService;
import com.dycx.user.vo.IndustryTypeVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service
@AllArgsConstructor
public class IndustryTypeImpl extends ServiceImpl<IndustryTypeMapper, IndustryTypeDO> implements IndustryTypeService {

    public Boolean delete(int typeId) {
        return this.removeById(typeId);
    }

    /**
     * 添加
     *
     * @param industryTypeDto
     * @return
     */
    @Override
    public Boolean add(IndustryTypeDTO industryTypeDto) {
        IndustryTypeDO industryTypeEntity = BeanUtil.toBean(industryTypeDto, IndustryTypeDO.class);

        boolean result = this.save(industryTypeEntity);
        industryTypeDto.setId(industryTypeEntity.getId());
        return result;
    }

    /**
     * 添加/修改
     *
     * @param industryTypeDto
     * @return
     */
    @Override
    public Boolean edit(IndustryTypeDTO industryTypeDto) {
        IndustryTypeDO industryTypeEntity = BeanUtil.toBean(industryTypeDto, IndustryTypeDO.class);
        return this.updateById(industryTypeEntity);
    }

    /**
     * 获取父角色下的所有角色
     *
     * @param parentId
     * @return
     */
    @Override
    public List<IndustryTypeVO> getAllList(Integer parentId) {
        LambdaQueryWrapper<IndustryTypeDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IndustryTypeDO::getParentId, parentId);
        queryWrapper.orderByDesc(IndustryTypeDO::getId);

        List<IndustryTypeDO> typeList = this.list(queryWrapper);

        List<IndustryTypeVO> industryTypeVoList = typeList.stream().map(item ->
                BeanUtil.toBean(item, IndustryTypeVO.class)
        ).collect(Collectors.toList());

        for (IndustryTypeVO industryTypeVo : industryTypeVoList) {
            List<IndustryTypeVO> childrenList = getAllList(industryTypeVo.getId());
            if (childrenList != null && childrenList.size() > 0) {
                industryTypeVo.setChildren(childrenList);
            }
        }

        return industryTypeVoList;
    }

    /**
     * 更新排序
     *
     * @param industryTypeDTO
     * @return
     */
    @Override
    public Boolean updateSort(IndustryTypeDTO industryTypeDTO) {
        IndustryTypeDO industryTypeEntity = new IndustryTypeDO();
        industryTypeEntity.setId(industryTypeDTO.getId());
        industryTypeEntity.setSort(industryTypeDTO.getSort());
        return this.updateById(industryTypeEntity);
    }

    /**
     * 更新状态
     *
     * @param industryTypeDTO
     * @return
     */
    @Override
    public Boolean updateStatus(IndustryTypeDTO industryTypeDTO) {
        IndustryTypeDO industryTypeEntity = new IndustryTypeDO();
        industryTypeEntity.setId(industryTypeDTO.getId());
        industryTypeEntity.setStatus(industryTypeDTO.getStatus());
        return this.updateById(industryTypeEntity);
    }

    /**
     * 获取分类id父路径
     *
     * @param id
     * @return
     */
    @Override
    public List<Integer> getIndustryTypeIdLevel(int id) {
        List<Integer> idList = new ArrayList<>();

        IndustryTypeDO articleTypeEntity = this.getById(id);

        if (articleTypeEntity.getParentId() != 0) {
            idList.addAll(this.getIndustryTypeIdLevel(articleTypeEntity.getParentId()));
        }

        idList.add(id);
        return idList;
    }

    /**
     * 查询父类下第一级分类
     *
     * @param parentId
     * @return
     */
    @Override
    public List<IndustryTypeVO> getListByParentId(Integer parentId) {
        LambdaQueryWrapper<IndustryTypeDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IndustryTypeDO::getParentId, parentId);
        queryWrapper.orderByDesc(IndustryTypeDO::getSort);

        List<IndustryTypeDO> typeList = this.list(queryWrapper);

        List<IndustryTypeVO> industryTypeVOList = new ArrayList<>();

        typeList.forEach(item -> {
            IndustryTypeVO industryTypeVO = BeanUtil.toBean(item, IndustryTypeVO.class);

            //是否有子类
            LambdaQueryWrapper<IndustryTypeDO> queryCount = new LambdaQueryWrapper<>();
            queryCount.eq(IndustryTypeDO::getParentId, item.getId());
            industryTypeVO.setHasChildren(count(queryCount) > 0);

            industryTypeVOList.add(industryTypeVO);
        });

        return industryTypeVOList;
    }
}
